<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.13"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>tutorial/csharp/MonitorCanChannel.cs</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="resize.js"></script>
<script type="text/javascript" src="navtreedata.js"></script>
<script type="text/javascript" src="navtree.js"></script>
<script type="text/javascript">
  $(document).ready(initResizable);
</script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/searchdata.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td id="projectlogo"><img alt="Logo" src="kvaser.gif"/></td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.13 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "search",false,'Search');
</script>
<script type="text/javascript" src="menudata.js"></script>
<script type="text/javascript" src="menu.js"></script>
<script type="text/javascript">
$(function() {
  initMenu('',true,false,'search.php','Search');
  $(document).ready(function() { init_search(); });
});
</script>
<div id="main-nav"></div>
</div><!-- top -->
<div id="side-nav" class="ui-resizable side-nav-resizable">
  <div id="nav-tree">
    <div id="nav-tree-contents">
      <div id="nav-sync" class="sync"></div>
    </div>
  </div>
  <div id="splitbar" style="-moz-user-select:none;" 
       class="ui-resizable-handle">
  </div>
</div>
<script type="text/javascript">
$(document).ready(function(){initNavTree('tutorial_2csharp_2_monitor_can_channel_8cs-example.html','');});
</script>
<div id="doc-content">
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>

<div class="header">
  <div class="headertitle">
<div class="title">tutorial/csharp/MonitorCanChannel.cs</div>  </div>
</div><!--header-->
<div class="contents">
<div class="fragment"><div class="line"><span class="keyword">using</span> System;</div><div class="line"><span class="keyword">using</span> System.Collections.Generic;</div><div class="line"><span class="keyword">using</span> System.Linq;</div><div class="line"><span class="keyword">using</span> System.Text;</div><div class="line"><span class="keyword">using</span> Kvaser.CanLib;</div><div class="line"></div><div class="line"><span class="keyword">namespace </span>MonitorCanChannel</div><div class="line">{</div><div class="line">    <span class="keyword">class </span>Program</div><div class="line">    {</div><div class="line">        <span class="keyword">static</span> <span class="keywordtype">void</span> Main(<span class="keywordtype">string</span>[] args)</div><div class="line">        {</div><div class="line">            <span class="comment">// Holds a handle to the CAN channel</span></div><div class="line">            <span class="keywordtype">int</span> handle;</div><div class="line"></div><div class="line">            <span class="comment">// Status returned by the Canlib calls</span></div><div class="line">            Canlib.canStatus status;</div><div class="line"></div><div class="line">            <span class="comment">// The CANlib channel number we would like to use</span></div><div class="line">            <span class="keywordtype">int</span> channelNumber = 0;</div><div class="line"></div><div class="line">            <span class="comment">// Initialize the Canlib library with a call to</span></div><div class="line">            <span class="comment">// Canlib.initializeLibrary(). This always needs to be done before</span></div><div class="line">            <span class="comment">// doing anything with the library.</span></div><div class="line">            Canlib.canInitializeLibrary();</div><div class="line"></div><div class="line">            <span class="comment">// Next, we open up the channel and receive a handle to</span></div><div class="line">            <span class="comment">// it. Depending on what devices you have connected to your</span></div><div class="line">            <span class="comment">// computer, you might want to change the channel number. The</span></div><div class="line">            <span class="comment">// canOPEN_ACCEPT_VIRTUAL flag means that it is ok to open the</span></div><div class="line">            <span class="comment">// selected channel, even if it is on a virtual device.</span></div><div class="line">            handle = Canlib.canOpenChannel(channelNumber, Canlib.canOPEN_ACCEPT_VIRTUAL);</div><div class="line">            CheckStatus((Canlib.canStatus)handle, <span class="stringliteral">&quot;canOpenChannel&quot;</span>);</div><div class="line"></div><div class="line">            <span class="comment">// Once we have successfully opened a channel, we need to set its bitrate. We</span></div><div class="line">            <span class="comment">// do this using canSetBusParams. CANlib provides a set of predefined bus parameter</span></div><div class="line">            <span class="comment">// settings in the form of canBITRATE_XXX constants. For other desired bus speeds</span></div><div class="line">            <span class="comment">// bus paramters have to be set manually.</span></div><div class="line">            <span class="comment">// See CANlib documentation for more information on parameter settings.</span></div><div class="line">            status = Canlib.canSetBusParams(handle, Canlib.canBITRATE_250K, 0, 0, 0, 0);</div><div class="line">            CheckStatus(status, <span class="stringliteral">&quot;canSetBusParams&quot;</span>);</div><div class="line"></div><div class="line">            <span class="comment">// Next, take the channel on bus using the canBusOn method. This</span></div><div class="line">            <span class="comment">// needs to be done before we can send a message.</span></div><div class="line">            status = Canlib.canBusOn(handle);</div><div class="line">            CheckStatus(status, <span class="stringliteral">&quot;canBusOn&quot;</span>);</div><div class="line"></div><div class="line">            <span class="comment">//Start dumping messages on the console</span></div><div class="line">            DumpMessageLoop(handle);</div><div class="line"></div><div class="line">            <span class="comment">// Once we are done using the channel, we go off bus using the</span></div><div class="line">            <span class="comment">// canBusOff method. It take the handle as the only argument.</span></div><div class="line">            status = Canlib.canBusOff(handle);</div><div class="line">            CheckStatus(status, <span class="stringliteral">&quot;canBusOff&quot;</span>);</div><div class="line"></div><div class="line">            <span class="comment">// We also close the channel using the canCloseChannel method,</span></div><div class="line">            <span class="comment">// which take the handle as the only argument.</span></div><div class="line">            status = Canlib.canClose(handle);</div><div class="line">            CheckStatus(status, <span class="stringliteral">&quot;canClose&quot;</span>);</div><div class="line"></div><div class="line"></div><div class="line">            <span class="comment">// Wait for the user to press a key before exiting, in case the</span></div><div class="line">            <span class="comment">// console closes automatically on exit.</span></div><div class="line">            Console.WriteLine(<span class="stringliteral">&quot;Channel closed. Press any key to exit&quot;</span>);</div><div class="line">            Console.ReadKey();</div><div class="line">        }</div><div class="line"></div><div class="line"></div><div class="line">        <span class="keyword">private</span> <span class="keyword">static</span> <span class="keywordtype">void</span> DumpMessageLoop(<span class="keywordtype">int</span> handle)</div><div class="line">        {</div><div class="line">            Canlib.canStatus status;</div><div class="line">            <span class="keywordtype">bool</span> finished = <span class="keyword">false</span>;</div><div class="line"></div><div class="line">            <span class="comment">// Declare some variables for holding the incoming messages. The</span></div><div class="line">            <span class="comment">// incoming messages consist of the same parameters as an outgoing</span></div><div class="line">            <span class="comment">// message, i.e. identifier (id), body (data), length (dlc), and</span></div><div class="line">            <span class="comment">// flags), as well as a timestamp.</span></div><div class="line">            byte[] data = <span class="keyword">new</span> byte[8];</div><div class="line">            <span class="keywordtype">int</span> id;</div><div class="line">            <span class="keywordtype">int</span> dlc;</div><div class="line">            <span class="keywordtype">int</span> flags;</div><div class="line">            <span class="keywordtype">long</span> timestamp;</div><div class="line"></div><div class="line">            Console.WriteLine(<span class="stringliteral">&quot;Channel opened. Press Escape to close. &quot;</span>);</div><div class="line">            Console.WriteLine(<span class="stringliteral">&quot;ID  DLC DATA                      Timestamp&quot;</span>);</div><div class="line"></div><div class="line">            <span class="comment">// Start a loop that loops until a key is pressed or an unexpected</span></div><div class="line">            <span class="comment">// error occur.</span></div><div class="line">            <span class="keywordflow">while</span> (!finished)</div><div class="line">            {</div><div class="line">                <span class="comment">// Call the canReadWait method to wait for a message on the</span></div><div class="line">                <span class="comment">// channel. This method has a timeout parameter which in this</span></div><div class="line">                <span class="comment">// case is set to 100 ms. If a message is received during this</span></div><div class="line">                <span class="comment">// time, it will return the status code canOK and the message</span></div><div class="line">                <span class="comment">// will be written to the output parameters. If no message is</span></div><div class="line">                <span class="comment">// received, it will return canERR_NOMSG.</span></div><div class="line">                status = Canlib.canReadWait(handle, out <span class="keywordtype">id</span>, data, out dlc, out flags, out timestamp, 100);</div><div class="line"></div><div class="line">                <span class="comment">// If a message was received, display it</span></div><div class="line">                <span class="keywordflow">if</span> (status == Canlib.canStatus.canOK)</div><div class="line">                {</div><div class="line">                    DumpMessage(<span class="keywordtype">id</span>, data, dlc, flags, timestamp);</div><div class="line">                }</div><div class="line"></div><div class="line">                <span class="comment">// Call DisplayError and exit in case an actual error occurs</span></div><div class="line">                <span class="keywordflow">else</span> <span class="keywordflow">if</span> (status != Canlib.canStatus.canERR_NOMSG)</div><div class="line">                {</div><div class="line">                    CheckStatus(status, <span class="stringliteral">&quot;canReadWait&quot;</span>);</div><div class="line">                    finished = <span class="keyword">true</span>;</div><div class="line">                }</div><div class="line"></div><div class="line">                <span class="comment">// End the loop if the user presses the Escape key</span></div><div class="line">                <span class="keywordflow">if</span> (Console.KeyAvailable)</div><div class="line">                {</div><div class="line">                    <span class="keywordflow">if</span> (Console.ReadKey(<span class="keyword">true</span>).Key == ConsoleKey.Escape)</div><div class="line">                    {</div><div class="line">                        finished = <span class="keyword">true</span>;</div><div class="line">                    }</div><div class="line">                }</div><div class="line">            }</div><div class="line">        }</div><div class="line"></div><div class="line"></div><div class="line">        <span class="comment">//Prints an incoming message to the screen</span></div><div class="line">        <span class="keyword">private</span> <span class="keyword">static</span> <span class="keywordtype">void</span> DumpMessage(<span class="keywordtype">int</span> <span class="keywordtype">id</span>, byte[] data, <span class="keywordtype">int</span> dlc, <span class="keywordtype">int</span> flags, <span class="keywordtype">long</span> timestamp)</div><div class="line">        {</div><div class="line">            <span class="keywordflow">if</span> ((flags &amp; Canlib.canMSG_ERROR_FRAME) != 0)</div><div class="line">            {</div><div class="line">                Console.WriteLine(<span class="stringliteral">&quot;Error Frame received ****&quot;</span>);</div><div class="line">            }</div><div class="line">            <span class="keywordflow">else</span></div><div class="line">            {</div><div class="line">                Console.WriteLine(<span class="stringliteral">&quot;{0}  {1}  {2:x2} {3:x2} {4:x2} {5:x2} {6:x2} {7:x2} {8:x2} {9:x2}    {10}&quot;</span>,</div><div class="line">                                                 <span class="keywordtype">id</span>, dlc, data[0], data[1], data[2], data[3], data[4],</div><div class="line">                                                 data[5], data[6], data[7], timestamp);</div><div class="line">            }</div><div class="line">        }</div><div class="line"></div><div class="line">        <span class="comment">//This method prints an error if something goes wrong</span></div><div class="line">        <span class="keyword">private</span> <span class="keyword">static</span> <span class="keywordtype">void</span> CheckStatus(Canlib.canStatus status, <span class="keywordtype">string</span> method)</div><div class="line">        {</div><div class="line">            <span class="keywordflow">if</span> (status &lt; 0)</div><div class="line">            {</div><div class="line">                Console.WriteLine(method + <span class="stringliteral">&quot; failed: &quot;</span> + status.ToString());</div><div class="line">            }</div><div class="line">        }</div><div class="line">    }</div><div class="line">}</div><div class="line"></div><div class="line"><span class="comment">/*</span></div><div class="line"><span class="comment">Exercises:</span></div><div class="line"><span class="comment"> - Start this program, then run the SendMessage.cs program from another</span></div><div class="line"><span class="comment">   tutorial. Make sure to modify one of the programs so they use different</span></div><div class="line"><span class="comment">   channel numbers which both should be connected.</span></div><div class="line"><span class="comment"></span></div><div class="line"><span class="comment"> - Send an error message to your program using the canMSG_ERROR_FRAME flag.</span></div><div class="line"><span class="comment"></span></div><div class="line"><span class="comment"> - The canReadSync method waits until there is a message on the channel, but</span></div><div class="line"><span class="comment">   doesn&#39;t read the message. The canRead method reads messages, but returns</span></div><div class="line"><span class="comment">   canERR_NOMSG immediately if there is no message on the channel. Rewrite the</span></div><div class="line"><span class="comment">   program to use these methods instead of canReadWait.</span></div><div class="line"><span class="comment">*/</span></div></div><!-- fragment --> </div><!-- contents -->
</div><!-- doc-content -->
<!-- HTML footer for doxygen 1.8.13-->
<!-- start footer part -->
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
  <ul>
    <li class="footer"> (canlib 5.38) Tue Feb 15 2022</li>
  </ul>
</div>
</body>
</html>
